//
//  playlistViewController.m
//  mumegui
//
//  Created by Niels Buekers on 12/11/10.
//  Copyright 2010 NB. All rights reserved.
//

#import "playlistViewController.h"
#import "cwRadioAppDelegate.h"
#import "songDetails.h"

@implementation playlistViewController
@synthesize menu2,tabel1,song1,listOfSongs;

#pragma mark -
#pragma mark Initialization



-(void) setPlaylist:(NSMutableArray*) pl{
	listOfSong=pl;
	for (NSInteger i=0; i<[pl count ]; i=i+1) {
		[[listOfSong objectAtIndex:i] setPointerType:[[pl objectAtIndex:i] getArtist] stream:[[pl objectAtIndex:i] getStream] url:[[pl objectAtIndex:i] getUrl] album:[[pl objectAtIndex:i] getAlbum] coverUrl:[[pl objectAtIndex:i] getCoverUrl] title:[[pl objectAtIndex:i] getTitle]];
	}
	 		//listOfSongs=pl;
	if(listOfSongs != nil){
		[self updateSonglist];
		[self.tabel1 reloadData];
	}
}

-(void) updatePlaylist:(NSMutableArray*) pl{
	songDetails* sd=[[songDetails alloc]initWithArtist:[[NSString alloc] initWithString:[[listOfSong objectAtIndex:0] getArtist]]
												 title:[[NSString alloc] initWithString:[[listOfSong objectAtIndex:0] getTitle]] 
												 album:[[NSString alloc] initWithString:[[listOfSong objectAtIndex:0] getAlbum]]
								coverUrl:[[NSString alloc] initWithString:[[listOfSong objectAtIndex:0] getCoverUrl]]
												 genre:[[listOfSong objectAtIndex:0] getGenre]
												   url:[[NSString alloc] initWithString:[[listOfSong objectAtIndex:0] getUrl]]
												stream:[[NSString alloc] initWithString:[[listOfSong objectAtIndex:0] getStream]]
												   _id:[[listOfSong objectAtIndex:0] getId]
											  duration:[[listOfSong objectAtIndex:0] getDuration]];	

	[sd setCover:(UIImage*)[[NSData alloc]initWithData:(NSData*) [[listOfSong objectAtIndex:0] getCover] ]];
	[listOfSong removeAllObjects];
	[listOfSong addObject:sd];
	[listOfSong addObjectsFromArray:pl];
					 
	for (NSInteger i=1; i<[pl count ]; i=i+1) {
		[[listOfSong objectAtIndex:i] setPointerType:[[pl objectAtIndex:i] getArtist] stream:[[pl objectAtIndex:i] getStream] url:[[pl objectAtIndex:i] getUrl] album:[[pl objectAtIndex:i] getAlbum] coverUrl:[[pl objectAtIndex:i] getCoverUrl] title:[[pl objectAtIndex:i] getTitle]];
		
	}
	
	if(listOfSongs != nil){
		[self updateSonglist];
		[self.tabel1 reloadData];
	}
}


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	[self updateSonglist];
}

- (void) updateSonglist{
	if(listOfSong==nil){
		song1 = [[songDetails alloc] initWithArtist:@"Red Hot Chili Peppers" title:@"Snow" album:@"Stadium Arcadium" coverUrl:@"" genre:rock url:@"" stream:@"" _id:1 duration:1000  ];
		song2 = [[songDetails alloc] initWithArtist:@"Kommil Foo" title:@"Schone Schijn" album:@"Lof der waanzin" coverUrl:@"" genre:hiphop url:@"" stream:@"" _id:1 duration:1000];
		song3 = [[songDetails alloc] initWithArtist:@"Swedish House Mafia" title:@"One" album:@"Until One" coverUrl:@"" genre:classic url:@"" stream:@"" _id:1 duration:1000];
		UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rhcp" ofType:@"jpg"]];
		[song1 setCover:img];
		img  = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"kommilfoo" ofType:@"jpg"]];
		[song2 setCover:img];
		img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"coverOne" ofType:@"jpg"]];
		[song3 setCover:img];
	
		//Add items
		listOfSongs = [[NSMutableArray alloc] init];
		[listOfSongs addObject:song1];
		[listOfSongs addObject:song2];
		[listOfSongs addObject:song3];
		
	
	}else {
		listOfSongs=listOfSong;
	}
	
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	return [listOfSongs count];
}


- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
	
	CGRect CellFrame = CGRectMake(0, 0, 300, 60);
	CGRect Label1Frame = CGRectMake(75, 10, 290, 20);
	CGRect Label2Frame = CGRectMake(75, 30, 290, 15);
	CGRect Label3Frame = CGRectMake(75, 45, 290, 15);
	UILabel *lblTemp;
	
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
	
	//Initialize Label with tag 1.
	lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
	lblTemp.tag = 1;
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	//Initialize Label with tag 2.
	lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
	lblTemp.tag = 2;
	lblTemp.font = [UIFont boldSystemFontOfSize:12];
	lblTemp.textColor = [UIColor lightGrayColor];
	[cell.contentView addSubview:lblTemp];
	
	//Initialize Label with tag 3.
	lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
	lblTemp.tag = 3;
	lblTemp.font = [UIFont boldSystemFontOfSize:12];
	lblTemp.textColor = [UIColor lightGrayColor];
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	//doorklikpijl
	[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
	
	return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	return 60;
}

// Customize the appearance of a table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"Cell";		
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if(cell == nil)
		cell = [self getCellContentView:CellIdentifier];
		
	UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
	UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
	UILabel *lblTemp3 = (UILabel *)[cell viewWithTag:3];
	
	//setting a row
	lblTemp1.text = [(songDetails*)[listOfSongs objectAtIndex:indexPath.row] title];
	lblTemp2.text = [(songDetails*)[listOfSongs objectAtIndex:indexPath.row] artist];
	lblTemp3.text = [(songDetails*)[listOfSongs objectAtIndex:indexPath.row] album];
	int genre = [(songDetails*)[listOfSongs objectAtIndex:indexPath.row] genre];
	
	switch (genre) {
		case 0:
			[[cell imageView] setImage:[UIImage imageNamed:@"rock.png"]];
			break;
		case 1:
			[[cell imageView] setImage:[UIImage imageNamed:@"metal.png"]];
			break;
		case 2:
			[[cell imageView] setImage:[UIImage imageNamed:@"soundtrack.png"]];
			break;
		case 3:
			[[cell imageView] setImage:[UIImage imageNamed:@"reggae.png"]];
			break;
		case 4:
			[[cell imageView] setImage:[UIImage imageNamed:@"hiphop.png"]];
			break;
		case 5:
			[[cell imageView] setImage:[UIImage imageNamed:@"classic.png"]];
			break;			
		default:
			break;
	}

	
	return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if (indexPath.row<[listOfSongs count]) {
	
	songDetailsViewController *detailViewController = [[songDetailsViewController alloc] initWithNibName:@"songDetails" bundle:[NSBundle mainBundle]];
	
	[detailViewController setSong:[listOfSongs objectAtIndex:indexPath.row]];

	detailViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:detailViewController animated:YES];
	[detailViewController release];	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[listOfSongs release];
	[listOfSong release];
    [super dealloc];
}


@end

